package com.example.limit.controller;

import com.example.limit.service.PurchaseLimitService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/purchase")
public class PurchaseController {
    
    private final PurchaseLimitService limitService;
    
    public PurchaseController(PurchaseLimitService limitService) {
        this.limitService = limitService;
    }
    
    @PostMapping("/check")
    public String checkPurchaseLimit(@RequestParam Long userId, 
                                   @RequestParam String productId,
                                   @RequestParam String categoryId,
                                   @RequestParam int quantity) {
        boolean allowed = limitService.checkPurchaseLimit(userId, productId, categoryId, quantity);
        if (allowed) {
            return "允许购买";
        } else {
            return "超过限购数量";
        }
    }
    
    @PostMapping
    public String purchase(@RequestParam Long userId,
                          @RequestParam String productId,
                          @RequestParam String categoryId,
                          @RequestParam int quantity,
                          @RequestParam Long orderId) {
        boolean allowed = limitService.checkPurchaseLimit(userId, productId, categoryId, quantity);
        if (allowed) {
            // 这里应该调用订单服务创建订单，简化处理
            return "购买成功，订单ID: " + orderId;
        } else {
            return "购买失败：超过限购数量";
        }
    }
}